package data_access;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collection;

import modelo.Diagnostico;

public class DiagnosticosManager
{
	public static Diagnostico getById(int id)
	{
		Connection c = ConnectionFactory.getConnection();
		try
		{
			PreparedStatement ps = c
					.prepareStatement("SELECT nombre, descripcion FROM Diagnosticos WHERE id = ?");
			ps.setInt(1, id);
			ResultSet rs = ps.executeQuery();

			Diagnostico d = null;

			if (rs.next())
			{
				String name = rs.getString(1);
				String descripcion = rs.getString(2);
				d = new Diagnostico();
				d.setId(id);
				d.setNombre(name);
				d.setDescripcion(descripcion);
			}

			return d;
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			ConnectionFactory.closeConnection(c);
		}

		return null;
	}

	public static Collection<Diagnostico> getAll()
	{
		Connection c = ConnectionFactory.getConnection();
		try
		{
			PreparedStatement ps = c
					.prepareStatement("SELECT nombre, descripcion, id FROM Diagnosticos");
			ResultSet rs = ps.executeQuery();

			Collection<Diagnostico> cd = new ArrayList<Diagnostico>();
			while (rs.next())
			{
				String name = rs.getString(1);
				String descripcion = rs.getString(2);
				int id = rs.getInt(3);

				Diagnostico d = new Diagnostico();
				d.setNombre(name);
				d.setDescripcion(descripcion);
				d.setId(id);

				cd.add(d);
			}

			return cd;
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			ConnectionFactory.closeConnection(c);
		}

		return null;
	}

	public static Diagnostico getByName(String pattern)
	{
		Connection c = ConnectionFactory.getConnection();
		try
		{
			PreparedStatement ps = c
					.prepareStatement("SELECT id, nombre, descripcion FROM Diagnosticos WHERE nombre LIKE ?");
			ps.setString(1, "%" + pattern + "%");
			ResultSet rs = ps.executeQuery();

			Diagnostico d = null;

			if (rs.next())
			{
				int id = rs.getInt(1);
				String nombre = rs.getString(2);
				String descripcion = rs.getString(3);

				d = new Diagnostico();
				d.setId(id);
				d.setNombre(nombre);
				d.setDescripcion(descripcion);
			}

			return d;
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			ConnectionFactory.closeConnection(c);
		}

		return null;
	}

	public static int insertDiagnostico(String nombre, String descripcion)
	{
		Connection c = ConnectionFactory.getConnection();
		try
		{
			String sql = "INSERT INTO Diagnosticos (nombre, descripcion) VALUES (?, ?)";
			PreparedStatement ps = c.prepareStatement(sql,
					Statement.RETURN_GENERATED_KEYS);
			ps.setString(1, nombre);
			ps.setString(2, descripcion);
			ps.executeUpdate();

			ResultSet rs = ps.getGeneratedKeys();
			if (rs.next())
			{
				return rs.getInt(1);
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			ConnectionFactory.closeConnection(c);
		}
		return -1;
	}

	/*
	 * Guarda un diagnostico si no existe y devuelve su id. Si ya existe,
	 * devuelve su id.
	 */
	public static int Save(Diagnostico diagnostico)
	{
		if (diagnostico.getId() != -1)
		{
			return diagnostico.getId();
		}
		else
		{
			return insertDiagnostico(diagnostico.getNombre(),
					diagnostico.getDescripcion());
		}
	}
}